package my1024.app;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.KeyboardFocusManager;

import javax.media.j3d.Canvas3D;
import javax.swing.JPanel;

import my1024.controller.InputProcessing;
import my1024.controller.LogicController;
import my1024.view.RenderingManager;

import com.sun.j3d.utils.universe.SimpleUniverse;

/**
 * minhvv, p18, IFI, TPE 01/2014 - 07/2014
 */

public class MainApp {
	private LogicController logicController = null;
	private RenderingManager renderingManager = null;
	private InputProcessing inputProcessing = null;

	private Canvas3D canvas = null;
	public JPanel drawingPanel = null;

	public MainApp() {
		Const.loadTextureToPool();
		setupDrawingPanel();
		initMVC();
		initKeyListener();
	}

	private void setupDrawingPanel() {
		drawingPanel = new JPanel();
		drawingPanel.setLayout(new BorderLayout());
		drawingPanel.setPreferredSize(new Dimension(600, 600));

		GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
		this.canvas = new Canvas3D(config);
		drawingPanel.add(canvas, BorderLayout.CENTER);
	}

	private void initMVC() {
		logicController = new LogicController();
		renderingManager = new RenderingManager(canvas);
		logicController.addObserver(renderingManager);
	}

	private void initKeyListener() {
		inputProcessing = new InputProcessing(logicController);
		KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
		manager.addKeyEventDispatcher(inputProcessing);
	}
}
